
namespace Pixels.ContentTypeItems.DataLayer
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.DataLayer;
    using Pixels.Exceptions;

    internal class DataAccessLayer : IDataAccessLayer
    {
        private DbHelper helper;

        #region Constructors
        public DataAccessLayer()
        {
            helper = new CommonDbHelper();
        }
        public DataAccessLayer(string ConnectionStringName)
        {
            helper = new CommonDbHelper(ConnectionStringName);
        }
        #endregion

        #region Methods
        public IContentTypeItem ContentItemAddEdit(IContentTypeItem objContentItem)
        {
            try
            {
                IContentTypeItem _contentitem = ContentTypeItemsFetch(objContentItem.ContentTypeID.Value, objContentItem.ItemName);

                if (_contentitem == null)
                {
                    helper.ExecuteNonQuery(SqlQueryBuilder.GetInsertQuery(objContentItem), CommandType.Text);
                }
                else
                {
                    objContentItem.ContentTypeItemID = _contentitem.ContentTypeItemID;

                    helper.ExecuteNonQuery(SqlQueryBuilder.GetUpdateQuery(objContentItem, "ContentTypeItemID"), CommandType.Text);                    
                }

                return objContentItem;
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ContentItem could not be added/modified. Please Contact System Administrator");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public void ContentItemDelete(Guid ContentItemID)
        {
            try
            {
                helper.ExecuteNonQuery(DBConstants.ContentTypeItemDelete, ContentItemID);                
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ContentItem not be deleted");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public IContentTypeItem ContentItemFetch(Guid ContentItemID)
        {
            ContentTypeItem objContentItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ContentTypeItemFetchByID, ContentItemID))
                {
                    if ((reader != null) && (reader.Read()))
                    {
                        objContentItem = new ContentTypeItem();
                        LoadToContentItem(objContentItem, reader);
                    }
                    reader.Close();
                    return objContentItem;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ContentItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public IContentTypeItem ContentItemFetchByExtentedListItemID(Guid ExtentedListItemID)
        {
            ContentTypeItem objContentItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ContentTypeItemFetchByExtentedListItemID, ExtentedListItemID))
                {
                    if ((reader != null) && (reader.Read()))
                    {
                        objContentItem = new ContentTypeItem();
                        LoadToContentItem(objContentItem, reader);
                    }
                    reader.Close();
                    return objContentItem;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("ContentItem Detail could not be found");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public IList<IContentTypeItem> ContentItemsFetchByContentTypeID(Guid ContentTypeID)
        {
            IList<IContentTypeItem> ContentItems = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ContentTypeItemsFetchByContentTypeID, ContentTypeID))
                {
                    if ((reader != null))
                    {
                        ContentItems = new List<IContentTypeItem>();
                        while (reader.Read())
                        {
                            ContentTypeItem objContentItem = new ContentTypeItem();
                            LoadToContentItem(objContentItem, reader);
                            ContentItems.Add(objContentItem);
                        }
                    }
                    reader.Close();
                    return ContentItems;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("Error in Fetching ContentItem Details.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public IContentTypeItem ContentTypeItemsFetch(Guid ContentTypeID, string ItemName)
        {
            ContentTypeItem objContentItem = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ContentTypeItemsFetchByContentTypeID_ItemName, ContentTypeID, ItemName))
                {
                    if ((reader != null))
                    {

                        while (reader.Read())
                        {
                            objContentItem = new ContentTypeItem();
                            LoadToContentItem(objContentItem, reader);
                        }
                    }
                    reader.Close();
                    return objContentItem;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("Error in Fetching ContentItem Details.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public IList<IContentTypeItem> ContentItemsFetch()
        {
            IList<IContentTypeItem> ContentItems = null;
            try
            {
                using (IDataReader reader = helper.ExecuteReader(DBConstants.ContentTypeItemsFetch, CommandType.Text))
                {
                    if ((reader != null))
                    {
                        ContentItems = new List<IContentTypeItem>();

                        while (reader.Read())
                        {
                            ContentTypeItem objContentItem = new ContentTypeItem();
                            LoadToContentItem(objContentItem, reader);
                            ContentItems.Add(objContentItem);
                        }
                    }
                    reader.Close();
                    return ContentItems;
                }
            }
            catch (DbException DbEx)
            {
                bool rethrow = ExceptionPolicy.HandleException(DbEx, "Data Layer Policy");
                throw new DLWrappedException("Error in Fetching ContentItem Details.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        private void LoadToContentItem(ContentTypeItem objContentItem, IDataReader reader)
        {
            objContentItem.ContentTypeItemID = Guid.Parse(reader["ContentTypeItemID"].ToString());
            objContentItem.ItemName = reader["ItemName"].ToString();
            objContentItem.DisplayName = reader["DisplayName"].ToString();
            objContentItem.DefaultValue = reader["DefaultValue"].ToString();
            int size = 0;
            int.TryParse(reader["Size"].ToString(), out size);
            objContentItem.Size = size;
            objContentItem.Type = reader["Type"].ToString();
            objContentItem.ContentTypeID = Guid.Parse(reader["ContentTypeID"].ToString());
            Guid baseContentTypeItemID;
            Guid.TryParse(reader["BaseContentTypeItemID"] == null ? "" : reader["BaseContentTypeItemID"].ToString(), out baseContentTypeItemID);
            objContentItem.BaseContentTypeItemID = baseContentTypeItemID;
        }
        #endregion
    }
}
